import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../entity/home/content/red_packet_decoration_entity.dart';
import '../../../utils/Tools.dart';
import 'decoration_red_packet_ctr.dart';

class DecorationRedPacket extends StatefulWidget {
  final RedPacketDecorationEntity model;

  const DecorationRedPacket({super.key, required this.model});

  @override
  State<DecorationRedPacket> createState() => _DecorationRedPacketState();
}

class _DecorationRedPacketState extends State<DecorationRedPacket> {
  final DecorationRedPacketCtr controller = Get.put(DecorationRedPacketCtr());

  @override
  void initState() {
    super.initState();
    controller.fetchRedPacketData(widget.model); // 发起请求
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.dataList.isEmpty) {
        return const SizedBox(); // 数据为空时的状态
      }
      return _buildRedPacketItemWidget();
    });
  }

  Widget _buildRedPacketItemWidget() {
    return Wrap(
      runSpacing: 8,
      children: controller.dataList.map((item) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                decoration: const BoxDecoration(
                  color: Colors.transparent,
                  image: DecorationImage(
                    image: AssetImage("assets/images/redpack_bg.png"),
                    fit: BoxFit.cover,
                  ),
                ),
                // constraints: BoxConstraints(maxHeight: 200.w),
                margin: const EdgeInsets.only(left: 8, right: 8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Row(
                      children: [
                        //左
                        SizedBox(
                          width: 80.w,
                          height: 80.h,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                "￥${Tools.formatNumber(item!.price ?? 0)}",
                                style: const TextStyle(
                                  fontSize: 22,
                                  fontWeight: FontWeight.w700,
                                  color: Color.fromRGBO(210, 152, 64, 1),
                                ),
                              ),
                              Text(
                                item.content ?? '',
                                style: const TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.w300,
                                  color: Color.fromRGBO(210, 152, 64, 1),
                                ),
                              )
                            ],
                          ),
                        ),
                        //中
                        Container(
                          height: 80.h,
                          width: 170.w,
                          margin: EdgeInsets.fromLTRB(10.w, 0.w, 20.w, 0.w),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                item.redPacketName ?? '',
                                style: const TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.w700,
                                  color: Color.fromRGBO(251, 241, 216, 1),
                                ),
                              ),
                              Text(
                                goodTypeValueString(item.goodsTypeValue ?? 0),
                                style: const TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.w300,
                                  color: Color.fromRGBO(251, 241, 216, 1),
                                ),
                              ),
                              Text(
                                item.useDate ?? '',
                                style: const TextStyle(
                                  fontSize: 10,
                                  fontWeight: FontWeight.w300,
                                  color: Color.fromRGBO(251, 241, 216, 1),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),

                    //右【按钮】

                    Expanded(
                      child: Container(
                        margin: EdgeInsets.only(right: 8.w),
                        child: Column(
                          children: [
                            SizedBox(
                                height: 30,
                                child: TextButton(
                                  style: ButtonStyle(
                                    backgroundColor: WidgetStateProperty.all(Tools.fromHex("#F83F2D")),
                                    side: WidgetStateProperty.all(const BorderSide(color: Color.fromRGBO(251, 241, 216, 1), width: 1.0)),
                                  ),
                                  onPressed: () {
                                    controller.receiveRedPack(item.redPacketId!);
                                  },
                                  child: Text(
                                    item.hasGet == 0 ? '立即领取' : '已领取',
                                    style: const TextStyle(fontSize: 10, fontWeight: FontWeight.w300, color: Colors.white),
                                  ),
                                )),
                          ],
                        ),
                      ),
                    )
                  ],
                )),
          ],
        );
      }).toList(),
    );
  }

  String goodTypeValueString(int type) {
    switch (type) {
      case 0:
        return '全场商品';
      case 1:
        return '指定商品';
      case 2:
        return '指定分类';
      case 3:
        return '指定品牌';
      default:
        return '全场商品';
    }
  }
}
